<?php
include_once getenv('DOCUMENT_ROOT').'/capcliente/ACTION/ACTFactory.php';
include_once getenv('DOCUMENT_ROOT').'/capcliente/VOB/VOBFactory.php';
include_once getenv('DOCUMENT_ROOT')."/capcliente/VIEW/template/Format.php";
session_start();

$VOBf = new VOBFactory();
$ACTf = new ACTFactory();
$f = Format::getInstancia();

$objUtil = $ACTf->getUtil();
$objCliente = $VOBf->getCliente();
$objActionCliente = $ACTf->getClienteAction();

if (isset($_GET['acao']) && $_GET['acao'] == 'prepareUpdate' && isset($_GET['id'])!= null) {

    if(!is_numeric($_GET['id'])){
          header( "Location: clientes.php?q=Error");
          exit();
      }

      $id = str_replace(array("<", ">", "\\", "/", "=", "'", "?"), "", $_GET['id']);
	
      $objCliente->setId($id);
      $objCliente = $objActionCliente->obterPorId($objCliente);
     
      session_register("clienteUpdate");
      $_SESSION['clienteUpdate'] = $objCliente;
		
      header( "Location: cad_cliente.php?acao=update");

} elseif (isset($_GET['acao']) && $_GET['acao'] == 'prepareInfo' && isset($_GET['id'])!= null) {

      if(!is_numeric($_GET['id'])){
          header( "Location: clientes.php?q=Error");
          exit();
      }

      $id = str_replace(array("<", ">", "\\", "/", "=", "'", "?"), "", $_GET['id']);

      $objCliente->setId($id);
      $objCliente = $objActionCliente->obterPorId($objCliente);

      if($objCliente != null){
          session_register("clienteInfo");
          $_SESSION['clienteInfo'] = $objCliente;

          header( "Location: inf_cliente.php?acao=informar");
      } else {
          header( "Location: clientes.php?q=Error");
      }
      
} elseif (isset($_GET['acao']) && $_GET['acao'] == 'inserir') {

    $objCliente = $objUtil->copiarPropridades($_POST, $objCliente, true);
    $ok = $objActionCliente->salvar($objCliente);
    
    if ($ok) {
        header("Location: clientees.php?q=ok") ;
    } else {
        header("Location: clientees.php?q=error");
    }
	 
} elseif (isset($_GET['acao']) && $_GET['acao'] == 'update') {

    $objCliente->setId($_POST["ide_cliente"]);
    $objCliente = $objActionCliente->obterPorId($objCliente);
    $objCliente = $objUtil->copiarPropridades($_POST, $objCliente, true);
  
    $ok = $objActionCliente->salvar($objCliente);
        
    session_unregister("clienteUpdate");
    
    if ($ok) {
        header( "Location: clientes.php?q=ok") ;
    } else {
        header( "Location: clientes.php?q=error");
    }
}
?>
